package mrdk.example.helloword;

import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;



@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	
	ImageView selectedImage;  
    private Integer[] mImageIds = {
               
               R.drawable.colections,
       };
	private int xoay=0;
	private boolean mStopHandler = false;;
	private Handler mHandler = new Handler();
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
      
       setupClickOnGallery();
       setupButton();
       setupStop();
       
   }
   private void setupStop() {
	// TODO Auto-generated method stub
	Button btnStop = (Button)findViewById(R.id.btnStop);
	btnStop.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			mStopHandler = true;
			
		}
	});
}
private void setupButton() {
	  
	   
	   
	   Button btnPlay = (Button)findViewById(R.id.btnPlay);
	   btnPlay.setOnClickListener(new OnClickListener() {
		@SuppressLint("NewApi") @Override
		public void onClick(View v) {
			Random r = new Random();	
			int position =r.nextInt(mImageIds.length);
			selectedImage.setImageResource(mImageIds[position]);
			mStopHandler = false;
			   Runnable runnable = new Runnable() {
			       @SuppressLint("NewApi") @Override
			       public void run() {
			           // do your stuff - don't create a new runnable here!
			           if (!mStopHandler) {
			        	   xoay+=10;
			        	   selectedImage.setRotation(xoay);     
			              
						mHandler.postDelayed(this, 30);
			           }
			       }
			   };

			   // start it with:
			   mHandler.post(runnable);
		}
	});
	
}
/**
    * setup for on click on gallery
    * 
    */
private void setupClickOnGallery() {
		selectedImage=(ImageView)findViewById(R.id.imageView1);

}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
